//
//  QSVTSignInRecordViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/23.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSVTSignInRecordViewController.h"
#import "QSSignInRecordCell.h"
#import "QSSingInInterface.h"
#import "QSSignInRecordViewController.h"
#import "QSSignRecordDetailModel.h"
#import "QSVTSignHeaderView.h"
#import "QSVisitInfoCompanyListModel.h"
#import "BRPickerView.h"
#import <SDAutoLayout.h>
@interface QSVTSignInRecordViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, assign) NSInteger flag;
@property (nonatomic, strong) QSVTSignHeaderView *headerView;
@end

static NSString *signInRecordCell = @"QSSignInRecordCell";
@implementation QSVTSignInRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addRefreshHeader];
    [self addRefreshFooter];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth - 20, kScreenHeight - kNavgationBarHeight - kRealValue(68) - kRealValue(155));
    [self.tableView registerNib:[UINib nibWithNibName:@"QSSignInRecordCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:signInRecordCell];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    [self configPage];
    if (kCurrentUser.enterpriseLevel.integerValue == 1) {
        //组员配置筛选的头部
        [self.view addSubview:self.headerView];
//        self.tableView.tableHeaderView = self.headerView;
        [self configHeaderView];
    }
    [self startRefreshing];
}

- (void)configHeaderView{
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(kRealValue(10));
        make.height.offset(kRealValue(40));
    }];
    NSLog(@"W = %.1f--- H = %.1f", self.tableView.width,self.tableView.height);
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.offset(kScreenHeight - kNavgationBarHeight - kRealValue(68) - kRealValue(155) - kRealValue(44));
    }];
    WeakSelf(weakSelf);
    //服务员可以筛选企业
    _headerView.didSelectSearchCompanyBlock = ^(QSVTSignHeaderView * _Nonnull vtHeaderView){
        NSLog(@"%@", vtHeaderView.clearButton.currentTitle);
        weakSelf.companyArray = [NSMutableArray array];
        if (weakSelf.companyModelArray.count) {
            for (QSVisitInfoCompanyListModel *model in weakSelf.companyModelArray) {
                NSString *companyName = model.companyName;
                [weakSelf.companyArray addObject:companyName];
            }
            [BRStringPickerView showStringPickerWithTitle:@"请选择公司" dataSource:weakSelf.companyArray defaultSelValue:nil resultBlock:^(id selectValue) {
                for (QSVisitInfoCompanyListModel *model in weakSelf.companyModelArray) {
                    if ([model.companyName isEqualToString:selectValue]) {
                        //取出选中公司key赋值
                        weakSelf.companyKey = model.companyKey;
                        break;
                    }
                }
                vtHeaderView.companyTextLabel.text = selectValue;
                vtHeaderView.companyTextLabel.textColor = [UIColor QSColorBlack434343];
                [weakSelf tableViewShouldUpdateDataByPageIndex:1];
            }];
        }else{
            [QSAppKeyWindow showAutoHideHudWithText:@"当前组无绑定公司!"];
        }
    };
//    头部清空筛选
    _headerView.didClearButtonBlock = ^(QSVTSignHeaderView * _Nonnull vtHeaderView) {
        weakSelf.companyKey = @"";
        vtHeaderView.companyTextLabel.text = @"选择企业名称进行搜索";
        vtHeaderView.companyTextLabel.textColor = [UIColor colorWithHexString:@"#C1BFC2"];
        [weakSelf tableViewShouldUpdateDataByPageIndex:1];

    };
}

- (QSVTSignHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[QSVTSignHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
        _headerView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _headerView.layer.cornerRadius = 5;
        _headerView.userInteractionEnabled = YES;
        
    }
    return _headerView;
}
- (void)tableViewShouldUpdateDataByPageIndex:(NSInteger)pageIndex{
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    [QSSingInInterface getSignListWithPageIndex:pageIndex Flag:self.flag Level:kCurrentUser.enterpriseLevel.integerValue Epid:_epId Egid:(NSInteger)_egId CompanyKey:_companyKey AndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSSignRecordListModel * _Nonnull result) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            if (pageIndex == 1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:result.list];
            self.tableView.emptyDataSetSource = self;
            self.tableView.emptyDataSetDelegate = self;
            [self.tableView reloadData];
            [self.tableView qs_endRefreshing];
            if (pageIndex >= result.pages) {
//                self.tableView.mj_footer.hidden = YES;
                [self.tableView qs_endRefreshingWithNoMoreData];
            }
        }
    }];
}

- (void)configPage{
    if (_index == 0) {
        self.flag = 1;
    }else if (_index == 1){
        self.flag = 2;
    }else{
        self.flag = 3;
    }
}

#pragma mark - **************** UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    QSSignInRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:signInRecordCell];
    QSSignInRecordCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:signInRecordCell forIndexPath:indexPath];
//        cell = (QSSignInRecordCell *)[[[NSBundle  mainBundle]  loadNibNamed:@"QSSignInRecordCell" owner:self options:nil]  objectAtIndex:0];

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    QSSignRecordDetailModel *model = self.dataArray[indexPath.row];
    return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QSSignInRecordCell class] contentViewWidth:kScreenWidth - 20];
//    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
//切换
- (void)didSelectOpenListWithEpId:(NSInteger)epid Egid:(NSInteger)egid CompanyKey:(NSString *)companyKey{
    NSLog(@"\nepid = %ld, egid = %ld, companyKey = %@", epid, egid, companyKey);
}

#pragma mark - Api
- (void)getCompanyList{
    
}

#pragma mark - DZNEmptyDataSetSource
/**
 *  返回标题文字
 */
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"暂时还没有记录哦!";
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                     NSForegroundColorAttributeName: [UIColor QSColorBlack666666] };
    return [[NSAttributedString alloc]initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    scrollView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    return [UIImage imageNamed:@"ic_problem_null"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
